import { useCallback } from 'react'
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'
import type { AppState, AppDispatch } from '../index'
import { onLogin, getProjectList } from '../../api/user';
import {
    changeTheme,
    login
} from './actions';

export const useAppSelector: TypedUseSelectorHook<AppState> = useSelector

/**
 * 调用 increment
 */
export const useChangeTheme = () => {
    const dispatch = useDispatch<AppDispatch>()

    return useCallback(
        (theme: string) => {
            dispatch(changeTheme({ theme }))
        },
        [dispatch]
    )
}

/**
 * 调用登录
 */
 export const useLogin = () => {
    const dispatch = useDispatch<AppDispatch>()
    return useCallback(() => {
        onLogin({
            password: "123456",
            username: "admin"
        }).then((res: any) => {
            dispatch(login({ token: res.result.token }))
        })
        getProjectList({ sort: 'asc' }).then(res => {

        })
    },[dispatch])
}


/**
 * 获取theme的值
 */
export const useTheme = () => {
    const value = useAppSelector(state => state.user.theme)
    return value
}